//
//  YFixSizeMutableDictionary.m
//  mcf
//
//  Created by yuike6 yuike6 on 12-1-18.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "YFixSizeMutableDictionary.h"
#import "header.h"

@implementation YFixSizeMutableDictionary

@synthesize keys;

- (id)initWithCapacity:(int)cap
{
	if (nil != (self = [super init])) 
    {
		capacity = cap;
		kDictionary = [[NSMutableDictionary alloc] initWithCapacity:cap];
		keys = [[NSMutableArray alloc] initWithCapacity:cap];
	}
	return self;
}

- (id)objectForKey:(id)key
{
    
    @synchronized(self)
    {
        // Pull key out of age array and move to front, indicates recently used
        @try {
            
            NSUInteger index = [keys indexOfObject:key];
            if (index == NSNotFound) 
                return nil;
            
            if (index != 0) {
                [keys removeObjectAtIndex:index];
                [keys insertObject:key atIndex:0];
            }
            
            return [kDictionary objectForKey:key];
        }
        @catch (NSException *exception) {
            return nil;
        }
        @finally {
            
        }        
    }
	
}


- (void)setObject:(id)value forKey:(id)key
{
	// Update the age of the inserted object and delete the oldest if needed
    @synchronized(self)
    {
        @try {
            if (!key) {
                return;
            }
            NSUInteger index = [keys indexOfObject:key];
            if (index != 0) {
                if (index != NSNotFound) {
                    [keys removeObjectAtIndex:index];
                }
                [keys insertObject:key atIndex:0];
                
                if ([keys count] > capacity) {
                    id delKey = [keys lastObject];
                    [kDictionary removeObjectForKey:delKey];
                    [keys removeLastObject];
                }
            }
            
            [kDictionary setObject:value forKey:key];
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            
        }   
    }     
}

- (void)removeObjectForKey:(id)aKey
{
    @synchronized(self)
    {
        @try {
            
            if (!aKey) 
            {
                return;
            }
            
            if (!keys ||
                [keys count] <= 0) 
            {
                return;
            }
            
            if (!kDictionary ||
                [kDictionary count] <= 0) 
            {
                return;
            }
            
            if ([keys containsObject:aKey]) 
            {
                [keys removeObject:aKey];
            }
            
            if ([kDictionary valueForKey:aKey]) 
            {
                [kDictionary removeObjectForKey:aKey];
            }
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            
        }  
    }
}


- (void)dealloc
{
	YRelease(kDictionary);
    YRelease(keys);
	[super dealloc];
}

@end
